//Are you sure you want to delete this place?
var strdelete 	= LOCATIONS_DELETE;
var obj_show	= LOCATIONS_SHOW;
var not_data	= LOCATIONS_DATA;

function Locations()
{
	var obj=new Object();
	obj.arrOld=new Object();
	obj.arrLocation =  new Array();     // tat ca cac diem cua user duoc load vao mang nay
    obj.arrLocationPan =  new Array();  // tat ca cac diem duoc ve tren ban do
    obj.arrLocationGrid =  new Array(); // mang cac diem hien len grid   
    obj.arrViewRec =  new Array();      // mang cac diem duoc ve hinh vuong    
	obj.currentIndex;
	obj.checkCreateLoc;
    obj.flgProcess = true;              // flgProcess = true co nghia la dang xu li hien thi dia diem dung de chan hok cho goi ham ve tiep theo
    obj.firstLoad = 0;                  // 0 co nghia la bik load lan dau    
    
    // ĐậmPV: 2010-10-15 - Add Start
    obj.CurPage = 1; // Trang hiện tại
    // ĐậmPV: 2010-10-15 - Add End
        // chkLocArea_100609
    obj.CheckLocation=function(){
        if(obj.arrViewRec.length<=0){
            return;
        }
        for (i=0;i<obj.arrLocationGrid.length;i++)
        {              
            var key =  obj.arrLocationGrid[i];
            if(obj.arrViewRec[key]){
                document.getElementById('chkLocArea_'+key).checked=true; 
            } 
        }
    }
    
	obj.ShowAll = function(bCheck)
	{
        $("#tblLocationList INPUT[name='chkLocItem']").attr("checked", bCheck);
		for(var i in obj.arrLocation)
		{
            if(typeof(obj.arrLocation[i])!='function')
            {
			    obj.Show(i,bCheck);
            }
		}
	}
    
    
        
    obj.panLocation = function(){
       obj.ClearAll2(obj.arrLocationPan);
       obj.PanObjOnMap(); 
       obj.CreateAll2(obj.arrLocationPan);       
       obj.flgProcess= false; 
    }

  obj.PanObjOnMap=function(){
       var  tempPan = new Array();
        var bounds = Gmap.getBounds();
        var sw = bounds.getSouthWest();
        var ne = bounds.getNorthEast();
        latView1 =  sw.lat();
        latView2 =  ne.lat(); 
        longView1 =  sw.lng(); 
        longView2 =  ne.lng(); 
        var index=0;       
        if(obj.arrLocation){    
        for(var i in obj.arrLocation){
            var lat = (obj.arrLocation[i].lat1+obj.arrLocation[i].lat2)/2;
            var lng = (obj.arrLocation[i].lng1+obj.arrLocation[i].lng2)/2;
            if(lat>latView1 && lat<latView2 && lng>longView1 && lng < longView2){
              tempPan[index]=i;
              index++;  
            }
        }
         if(tempPan.length>500){
             obj.ClearAll2(obj.arrLocationPan);
             _global.myMap.HideInfo();
             obj.arrLocationPan =  new Array();
             return; 
         }
        }
        obj.arrLocationPan = tempPan;        
    }
    
    obj.arr_diff=function(a1, a2) 
    {
      var  diff=[],atemp2=[];

        index = 0;
        if(a2.length>0){
            for(i=0;i<a2.length;i++)
                atemp2[a2[i]]=true;
            for(i=0;i<a1.length;i++){
                if(!atemp2[a1[i]]){
                    diff[index]=a1[i];
                    index++;
                } 
            }
        }else{
          diff =a1;  
        }
      return diff;
    }
    
    obj.arr_thesame=function(a1, a2) 
    {
      var  diff=[],atemp2=[];

        index = 0;
        if(a2.length>0){
            for(i=0;i<a2.length;i++)
                atemp2[a2[i]]=true;
            for(i=0;i<a1.length;i++){                           
                if(atemp2[a1[i]]){
                    diff[index]=a1[i];
                    index++;
                } 
            }
        }         
      return diff;
    }
    
    obj.RemoveObjOnMap=function(){  
        obj.arrLocationRemove = new Array(); 
        if(obj.arrLocationPan.length==0){
            return;
        }
        var bounds = Gmap.getBounds();
        var sw = bounds.getSouthWest();
        var ne = bounds.getNorthEast();
        latView1 =  sw.lat();
        latView2 =  ne.lat(); 
        longView1 =  sw.lng(); 
        longView2 =  ne.lng(); 
        var index=0;    
        for(i=0;i<obj.arrLocationPan.length;i++){
            key = obj.arrLocationPan[i]; 
            var lat = (obj.arrLocation[key].lat1+obj.arrLocation[key].lat2)/2;
            var lng = (obj.arrLocation[key].lng1+obj.arrLocation[key].lng2)/2;
            if(!(lat>latView1 && lat<latView2 && lng>longView1 && lng < longView2)){
              obj.arrLocationRemove[index]=key;
              index++;  
            }
        }           
    }
    
     
	obj.CreateAll = function()
	{	
		if(typeof(obj.arrLocation)!='undefined' && obj.arrLocation!=null)
		{ 
			for(var i in obj.arrLocation)
			{
                if(typeof(obj.arrLocation[i])!='function')
                {
				    obj.arrLocation[i].lable =_global.myMap.Makeinfo(obj.arrLocation[i]);	
				    obj.arrLocation[i].loc=_global.myMap.DrawLocationRec(obj.arrLocation[i].lat1,obj.arrLocation[i].lng1,obj.arrLocation[i].lat2,obj.arrLocation[i].lng2);
				    obj.arrLocation[i].icon=_global.myMap.CreateIconMap(obj.arrLocation[i],true);                    
				    obj.Show(i,obj.arrLocation[i].checked);
                }
			}	
		} 		
	}
    
    obj.CreateAll2 = function(arr) {    
        if (!arr || arr.length == 0) {
            return;
        }
        if(obj.arrLocationPan.length>500){
             obj.arrLocationPan =  new Array();  
             return; 
         }
        var level = _global.myMap.GetLevel();  
        for (var i=0; i<arr.length; i++) {
            var key = arr[i];            
            if(typeof(obj.arrLocation[key])!='function') {
                if(obj.arrViewRec[key]){                    
                  obj.arrLocation[key].loc=_global.myMap.DrawLocationRec(obj.arrLocation[key].lat1,obj.arrLocation[key].lng1,obj.arrLocation[key].lat2,obj.arrLocation[key].lng2);
                }
                obj.arrLocation[key].icon = _global.myMap.CreateIconMap(obj.arrLocation[key], true);
                obj.Show2(key, true);
            }
        } 
    }
    
	// hàm tạo Icon cho địa điểm	
	obj.CreateIconAll = function(nCheck)
	{
		if(nCheck == true)
		{
			if(typeof(obj.arrLocation)!='undefined' && obj.arrLocation!=null)
			{
				for(var i in obj.arrLocation)
				{
                    if(typeof(obj.arrLocation[i])!='function')
                    {
					    obj.arrLocation[i].icon=_global.myMap.CreateIconMap(obj.arrLocation[i],nCheck);	
					    _global.myMap.HideLocation(obj.arrLocation[i].loc);
					    _global.myMap.HideLocation(obj.arrLocation[i].lable);
                    }
				}	
			} 
		}
		else
		{
			for(var i in obj.arrLocation)
				{
                    if(typeof(obj.arrLocation[i])!='function')
                    {
					    _global.myMap.HideIcon(obj.arrLocation[i].icon);					
					    _global.myMap.ShowLocation(obj.arrLocation[i].loc);
					    _global.myMap.ShowLocation(obj.arrLocation[i].lable);	
                    }				
				}	
		}
				
	}
	obj.ClearAll = function()
	{		    
		if(typeof(_global.myMap.locCreating)!='undefined')
		{
			_global.myMap.DeleteLocation(_global.myMap.locCreating)
		}
        
	    for(var i in obj.arrLocation)
	    {      
	        if( typeof(obj.arrLocation[i])!='function')
			{    if(typeof(obj.arrLocation[i].loc)!='undefined'){
                 _global.myMap.DeleteLocation(obj.arrLocation[i].loc); 
                  }
                _global.myMap.DeleteLocation(obj.arrLocation[i].icon);
			}
	    }	   		
	}
    obj.ClearAll2 = function(arr) { 
        if (!arr || arr.length == 0) {
            return;
        }
        for (var i=0; i<arr.length; i++) {
            var key = arr[i];
            if( typeof(obj.arrLocation[key])!='function') {
                if(typeof(obj.arrLocation[key].loc)!='undefined'){
                    _global.myMap.DeleteLocation(obj.arrLocation[key].loc);    
                }
                if(typeof(obj.arrLocation[key].icon)!='undefined'){
                    _global.myMap.DeleteLocation(obj.arrLocation[key].icon);
                }
               // _global.myMap.HideInfo();
            }
        }               
    }
    
    obj.Show2 = function(index,bCheck) {
        if(bCheck == true) {
            if(typeof(obj.arrLocation[index].icon)!='undefined') {                                
                GEvent.addListener(obj.arrLocation[index].icon, "click", function(event) {
                    var lat = (obj.arrLocation[index].lat1 + obj.arrLocation[index].lat2) / 2;
                    var lng = (obj.arrLocation[index].lng1 + obj.arrLocation[index].lng2) / 2;
                    var point = new GLatLng(lat, lng);     
                    obj.Infopolygon2(index); 
                } );
            }                    
        } 
    }
    
    obj.Show3 = function(index,bCheck) {
        _global.myMap.HideInfo();
         obj.SetHighlight(index);  
        if(bCheck) {
            obj.arrViewRec[index] =  true;          
            var lat = (obj.arrLocation[index].lat1 + obj.arrLocation[index].lat2) / 2;
            var lng = (obj.arrLocation[index].lng1 + obj.arrLocation[index].lng2) / 2;                    
            _global.myMap.PanTo(lat, lng);                  
        } else { 
             obj.arrViewRec[index] =  false;            
            if(typeof(obj.arrLocation[index].loc)!='undefined') {
                _global.myMap.DeleteLocation(obj.arrLocation[index].loc);
            }
            obj.arrLocation[index].icon = _global.myMap.CreateIconMap(obj.arrLocation[index], true);
             
        }
        obj.Show2(index, true); 
    }
    
	obj.checkIcon = false;
	obj.Show = function(index,bCheck)
    {        
        obj.arrLocation[index].checked=bCheck;
        if(obj.checkIcon == false)
        {
            if(bCheck == true)
            {
                if(typeof(obj.arrLocation[index].loc)!='undefined')
                {
                    _global.myMap.ShowLocation(obj.arrLocation[index].loc);
                    _global.myMap.ShowLocation(obj.arrLocation[index].lable);
                    ////////////
                    _global.myMap.HideIcon(obj.arrLocation[index].icon);
                    /////////////
                }                    
            }
            else
            {
                if(typeof(obj.arrLocation[index].loc)!='undefined')
                {
                    _global.myMap.HideLocation(obj.arrLocation[index].loc);
                    _global.myMap.HideLocation(obj.arrLocation[index].lable);
                    ////
                    _global.myMap.HideIcon(obj.arrLocation[index].icon);
                    ////
                }    
            }
        }
        else
        {
            if(bCheck == true)
            {
                if(typeof(obj.arrLocation[index].loc)!='undefined')
                {
                    _global.myMap.ShowIcon(obj.arrLocation[index].icon);    
                    _global.myMap.HideLocation(obj.arrLocation[index].loc);
                    _global.myMap.HideLocation(obj.arrLocation[index].lable);
                    var latlng = _global.myMap.GetCenter();
                    _global.myMap.PanTo(latlng.latitude,latlng.longitude);   
                }
            }
            else
            {
                if(typeof(obj.arrLocation[index].loc)!='undefined')
                {
                    _global.myMap.HideLocation(obj.arrLocation[index].loc);
                    _global.myMap.HideLocation(obj.arrLocation[index].lable);
                    _global.myMap.HideIcon(obj.arrLocation[index].icon);
                }    
            }
        }
    }
             
	obj.GetLocation = function(index)
	{	
		if(!obj.arrLocation[index])
			return null;
		else
			return obj.arrLocation[index];
	}
	obj.IsChecked = function(index)
	{	
		if(obj.arrLocation[index].checked == true)
		{	
			return true;
		}	
		else
			return false;
	}
	obj.Add = function(index)
	{		
		obj.arrLocation.push(index);
	}
	obj.Remove = function(index)
	{
		obj.arrLocation.splice(index,1);
	}
	obj.Refresh = function()
	{
	    for(var i in obj.arrLocation)
	    {
	        obj.Show(i,obj.arrLocation[i].checked);
	    }	
	}
	obj.Delete = function(index)  // ajax
	{
		if(confirm(strdelete))
		{ 
                var arrParams = new Array();
        arrParams['id'] = index;    
         xajax_ajxDelLoc(arrParams);
		}
	}
    
    obj.DeleteRowAndIcon = function(index)  // ajax
    {
        $('#trlocation'+index).css('display', 'none'); 
        var arrtemp= new Array();
        arrtemp[0]= index;
        obj.ClearAll2(arrtemp);
        _global.myMap.HideInfo();
        obj.arrLocation[index] = false;
      

    }
    
	obj.PanTo=function(index)
	{
 
        if (typeof(obj.arrLocation[index])!='undefined')
        {
            obj.SetHighlight(index);
            if(obj.arrLocation[index].checked==true)
		    {
			     _global.myMap.PanTo( (obj.arrLocation[index].lat1 + obj.arrLocation[index].lat2)/2 ,(obj.arrLocation[index].lng1 + obj.arrLocation[index].lng2)/2);
                 obj.currentIndex = index;
			}else{
			    alert(obj_show);
			}            
        } 
        else
            alert(not_data);        
	
	}
    obj.SetHighlight=function(index)
    {
        for (var i=0;i<obj.arrLocationGrid.length;i++)
        { 
            document.getElementById('trlocation'+obj.arrLocationGrid[i]).className="trreviewnormal";
        }
        document.getElementById('trlocation'+index).className="HightLight2";
        
    }
	obj.LoadDB=function()
	{   
        obj.arrOld=obj.arrLocation;
        obj.LoadLocList(obj.CurPage); 
	}	
	obj.UpdateChecked=function()
	{	    
	    for(var i in obj.arrLocation)
	    {
	        if(typeof(obj.arrOld[i])!='undefined' && typeof(obj.arrOld[i])!='function')
            {
	            obj.arrLocation[i].checked=obj.arrOld[i].checked;
	        }
	    }
	}
	obj.SaveLocByRec=function()
	{
        if (trim($("#txtLocNameByAddRec").val()) == '') 
        {
            alert(LOCATIONS_NOT_INPUT_L);
            $("#txtLocNameByAddRec").focus();
            return;
        }
        
		//obj.ClearAll();
        
        var arrParram=new Array();
                
        arrParram['Lat1']=$("#hdnLat1ByAddRec").val();
        arrParram['Lng1']=$("#hdnLng1ByAddRec").val();
        arrParram['Lat2']=$("#hdnLat2ByAddRec").val();
        arrParram['Lng2']=$("#hdnLng2ByAddRec").val();
                        
        arrParram['LocName']=$("#txtLocNameByAddRec").val(); // Ten Dia Diem
        arrParram['Address']=$("#txtAddressByAddRec").val(); // Dia Chi
        arrParram['LocType']=$("#ddlLocTypeByAddRec").val(); // Loai Dia Diem
        arrParram['Contact']=$("#txtContactByAddRec").val(); // Lien He
        arrParram['Phone']=$("#txtPhoneByAddRec").val(); // So Dien Thoai
        arrParram['Fee']=$("#txtFeeByAddRec").val(); // Chi phi
        arrParram['Desc']=$("#txtDescByAddRec").val(); // Ghi chu
                        
        arrParram['page'] = obj.CurPage;    // ĐậmPV: 2010-10-15 - Add: Lấy trang hiện tại
        
        xajax_ajxAddLoc(arrParram);
        
        _global.myMap.PolyDragControl.reset();
		_global.myMap.HideInfo();
	}
    
    obj.SaveLoc=function()
    {
        if (trim($("#txtLocName").val()) == '') 
        {
            alert(LOCATIONS_NOT_INPUT_L);
            $("#txtLocName").focus();
            return;
        }

        //obj.ClearAll();
        
        var arrParram=new Array();
        
        var rad = document.getElementById('txtRad').value;
        var lat = document.getElementById('clickLat').value;
        var lng = document.getElementById('clickLng').value;
                
        arrParram['Rad']=rad; // Rad
        arrParram['LatOrg']=lat; // Lat Org
        arrParram['LonOrg']=lng; // Lon Org
        var rec1 = _global.myMap.CreateRec(lat,lng,rad);
            
        arrParram['Lat1']=rec1.lat2;
        arrParram['Lng1']=rec1.lng1;
        arrParram['Lat2']=rec1.lat1;
        arrParram['Lng2']=rec1.lng2;
                        
        arrParram['LocName']=$("#txtLocName").val(); // Ten Dia Diem
        arrParram['Address']=$("#txtAddress").val(); // Dia Chi
        arrParram['LocType']=$("#ddlLocType").val(); // Loai Dia Diem
        arrParram['Contact']=$("#txtContact").val(); // Lien He
        arrParram['Phone']=$("#txtPhone").val(); // So Dien Thoai
        arrParram['Fee']=$("#txtFee").val(); // Chi phi
        arrParram['Desc']=$("#txtDesc").val(); // Ghi chu
                        
        arrParram['page'] = obj.CurPage;    // ĐậmPV: 2010-10-15 - Add: Lấy trang hiện tại
         
        xajax_ajxAddLoc(arrParram);
        
        _global.myMap.HideInfo();
    }
    
	obj.Infopolygon2 = function(key)
	{     
		for(var i in obj.arrLocation)
        {            
			if (i == key) {
                var infowind =  '<div class="infolocation" style="width:250px; height:150px;" align="left">' +
                        '<b>' + obj.arrLocation[i].name + '</b><br />' +
                        LOCATIONS_ADDRESS + obj.arrLocation[i].address + '<br />' +
                        LOCATIONS_TYPE + obj.arrLocation[i].loc_type + '<br />' +
                        LOCATIONS_CONTACT + obj.arrLocation[i].contact + '<br />' +
                        LOCATIONS_PHONE + obj.arrLocation[i].phone + '<br />' +
                        LOCATIONS_COST + obj.arrLocation[i].amount + '<br />' +
                        LOCATIONS_RADIUS + obj.arrLocation[i].rad + LOCATIONS_UNIT +
                        LOCATIONS_DECRIPTION + obj.arrLocation[i].description + '<br />' +
                        '</div>';

                var lat = (obj.arrLocation[i].lat1 + obj.arrLocation[i].lat2) / 2;
                var lng = (obj.arrLocation[i].lng1 + obj.arrLocation[i].lng2) / 2; 
                var point = new GLatLng(lat, lng);
                            
                _global.myMap.map.openInfoWindow(point, infowind);
                
                break;
			}	 
        }	
	} 
    
    obj.Infopolygon = function(point)
    {  
        for(var i in obj.arrLocation)
        {
            if (_global.myMap.Contains(obj.arrLocation[i].loc, point)) {
                var infowind =  '<div class="infolocation" style="width:250px; height:150px;" align="left">' +
                        '<b>' + obj.arrLocation[i].name + '</b><br />' +
                        LOCATIONS_ADDRESS + obj.arrLocation[i].address + '<br />' +
                        LOCATIONS_TYPE + obj.arrLocation[i].loc_type + '<br />' +
                        LOCATIONS_CONTACT + obj.arrLocation[i].contact + '<br />' +
                        LOCATIONS_PHONE + obj.arrLocation[i].phone + '<br />' +
                        LOCATIONS_COST + obj.arrLocation[i].amount + '<br />' +
                        LOCATIONS_RADIUS + obj.arrLocation[i].rad + LOCATIONS_UNIT +
                        LOCATIONS_DECRIPTION + obj.arrLocation[i].description + '<br />' +
                        '</div>';
                
                _global.myMap.map.openInfoWindow(point,infowind);
                
                break;
            }     
        }    
    }
           
    obj.LoadLocList = function(currPage)
    {
        var arrParams = new Array();        
        arrParams['CurrPage'] = currPage;
        arrParams['LocTypeId'] = document.getElementById('ddlLocType').value;
        arrParams['firstLoad'] = obj.firstLoad;        
        xajax_ajxGetLocList(arrParams);
    }    
    obj.LocTypeOnChanged = function()
    {           
        obj.LoadLocList(1);
    }
    obj.Edit = function(id)
    {
        
        xajax_ajxEditLoc(id);
    }    
    obj.ProcessSaveLoc = function()
    {
        if (trim($("#txtLocNameByEdit").val()) == '') 
        {
            alert(LOCATIONS_NOT_INPUT_L);
            $("#txtLocNameByEdit").focus();
            return;
        }
        
        var arrParram=new Array();
         
        arrParram['LocName']=$("#txtLocNameByEdit").val(); // Ten Dia Diem
        arrParram['Address']=$("#txtAddressByEdit").val(); // Dia Chi
        arrParram['LocType']=$("#ddlLocTypeByEdit").val(); // Loai Dia Diem
        arrParram['Contact']=$("#txtContactByEdit").val(); // Lien He
        arrParram['Phone']=$("#txtPhoneByEdit").val(); // So Dien Thoai
        arrParram['Fee']=$("#txtFeeByEdit").val(); // Chi phi
        arrParram['Desc']=$("#txtDescByEdit").val(); // Ghi chu        
        arrParram['page'] = obj.CurPage;    // ĐậmPV: 2010-10-15 - Add: Lấy trang hiện tại
        arrParram['id'] = $("#hdnLocIdByEdit").val();
        
        var rad = $("#txtRadByEdit").val();
        var lat = $("#hdnLatOrgByEdit").val(); // Lat Org
        var lng = $("#hdnLonOrgByEdit").val(); // Lon Org
                
        arrParram['Rad']=rad // Rad
        
        var rec1 = _global.myMap.CreateRec(lat,lng,rad);
            
        arrParram['Lat1']=rec1.lat1;
        arrParram['Lng1']=rec1.lng1;
        arrParram['Lat2']=rec1.lat2;
        arrParram['Lng2']=rec1.lng2;
        
        xajax_ajxSaveLoc(arrParram);
    } 
    
    // Add-Start by Lamdhn: YC101 - 2011.04.05
    obj.SaveLocByEdit = function() {
        if (document.getElementById('rdLocSquares').checked) {
            obj.ProcessSaveLoc();
        } else {
            obj.ProcessSaveLocByRec();
        }
    }
    
    obj.ProcessSaveLocByRec = function()
    {
        if (trim($("#txtLocNameByEdit").val()) == '') 
        {
            alert(LOCATIONS_NOT_INPUT_L);
            $("#txtLocNameByEdit").focus();
            return;
        }
       var arrParram=new Array();
         
        arrParram['LocName']=$("#txtLocNameByEdit").val(); // Ten Dia Diem
        arrParram['Address']=$("#txtAddressByEdit").val(); // Dia Chi
        arrParram['LocType']=$("#ddlLocTypeByEdit").val(); // Loai Dia Diem
        arrParram['Contact']=$("#txtContactByEdit").val(); // Lien He
        arrParram['Phone']=$("#txtPhoneByEdit").val(); // So Dien Thoai
        arrParram['Fee']=$("#txtFeeByEdit").val(); // Chi phi
        arrParram['Desc']=$("#txtDescByEdit").val(); // Ghi chu        
        arrParram['page'] = obj.CurPage;    // ĐậmPV: 2010-10-15 - Add: Lấy trang hiện tại
        arrParram['id'] = $("#hdnLocIdByEdit").val();
                            
        arrParram['Lat1'] = $("#hdnLat1ByEdit").val();
        arrParram['Lng1'] = $("#hdnLng1ByEdit").val();
        arrParram['Lat2'] = $("#hdnLat2ByEdit").val();
        arrParram['Lng2'] = $("#hdnLng2ByEdit").val();
        arrParram['LatOrg']=(parseFloat($("#hdnLat1ByEdit").val())+parseFloat($("#hdnLat2ByEdit").val()))/2; // Lat Org
        arrParram['LonOrg']=(parseFloat($("#hdnLng1ByEdit").val())+parseFloat($("#hdnLng2ByEdit").val()))/2; // Lon Org
        arrParram['Rad'] = 1;        
        xajax_ajxSaveLoc(arrParram);
    }
    // Add-End by Lamdhn: YC101 - 2011.04.05
                
	return obj;
}
